const app = getApp();
import { label_All, label_Server } from "../../api/homePage"
import { baseUrl } from "../../api/request"
import { getStorage } from "../../utils/localStorage"
Page({
    data: {
        navData: {
            title: '来自故乡的声音',
            level: 1
        },
        imgUrl: baseUrl,
        data: [
            {
                id: 1,
                title: '摄影1',
                src: '../../image/106.jpg'
            },
            {
                id: 2,
                title: '摄影2',
                src: '../../image/106.jpg'
            },
            {
                id: 3,
                title: '摄影3',
                src: '../../image/106.jpg'
            },
            {
                id: 4,
                title: '摄影4',
                src: '../../image/106.jpg'
            },
            {
                id: 5,
                title: '摄影5',
                src: '../../image/106.jpg'
            },
            {
                id: 6,
                title: '摄影6',
                src: '../../image/106.jpg'
            },
            {
                id: 7,
                title: '摄影7',
                src: '../../image/106.jpg'
            },
            {
                id: 8,
                title: '摄影8',
                src: '../../image/106.jpg'
            },
            {
                id: 9,
                title: '摄影9',
                src: '../../image/106.jpg'
            }, {
                id: 10,
                title: '摄影10',
                src: '../../image/106.jpg'
            },
        ],
        list: [
            {
                id: 1,
                title: '摄影1',
                src: '../../image/106.jpg'
            },
            {
                id: 2,
                title: '摄影2',
                src: '../../image/106.jpg'
            },
            {
                id: 3,
                title: '摄影3',
                src: '../../image/106.jpg'
            },
            {
                id: 4,
                title: '摄影4',
                src: '../../image/106.jpg'
            },
            {
                id: 5,
                title: '摄影5',
                src: '../../image/106.jpg'
            },
            {
                id: 6,
                title: '摄影6',
                src: '../../image/106.jpg'
            },
            {
                id: 7,
                title: '摄影7',
                src: '../../image/106.jpg'
            },
            {
                id: 8,
                title: '摄影8',
                src: '../../image/106.jpg'
            },
            {
                id: 9,
                title: '摄影9',
                src: '../../image/106.jpg'
            }, {
                id: 10,
                title: '摄影10',
                src: '../../image/106.jpg'
            },
        ],
        selectData: [],
        userInfo: {},
        pageTop: app.globalData.navHeight + app.globalData.navTop,
    },
    onLoad(options) {
        this.getList();
        this.setData({
            userInfo: getStorage("user")
        });
    },
    onReady() {

    },
    onShow() {
    },
    /* 获取列表数据 */
    getList() {
        label_All().then(res => {
            let { data } = res.data;
            let arr = [];
            const index = Math.ceil(data.length / 9); //获取数据展示的页数 一页展示9条
            for (let i = 0; i < index; i++) {
                arr.push({});
            };
            arr.forEach((item, index) => {
                item.data = data.slice(index * 9, index * 9 + 9);
            });
            this.setData({
                data: arr,
                list: data
            });
        });
    },
    /* 选择checkbox */
    choiceChange(e) {
        let _this = this.data;
        let data = e.detail.value;
        data = data.map(Number);  //转换成number数组
        let arr = [];
        _this.list.forEach(val => {
            data.forEach(item => {
                if (val.id === item) {
                    arr.push(val);
                };
            });
        });
        this.setData({
            selectData: arr
        });
    },
    //进入乡音按钮事件
    lentBtn(e) {
        if (this.data.selectData.length === 0) {
            wx.showToast({
                icon: "none",
                title: '请选择感兴趣的内容',
            });
        } else {
            let idArr = [];
            this.data.selectData.forEach(item => {
                idArr.push(item.id);
            });
            console.log(this.data.userInfo,'userinfo');
            label_Server({ userId: this.data.userInfo.id, labelId: idArr.join(",") }).then(res => {
                wx.redirectTo({ url: '/pages/index/index' });
            });
        };
    },
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})